package com.zztpc.ljl.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.DialogTitle;

import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;

import com.zztpc.ljl.myapplication.entity.CarList;
import com.zztpc.ljl.myapplication.entity.Guzhang;
import com.zztpc.ljl.myapplication.service.CarListService;
import com.zztpc.ljl.myapplication.service.GuzhangService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";//logt
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getInfo();
       // getCarInfo();
    }

//    private void getCarInfo() {
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl("http://zuowen.api.juhe.cn/")
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//        //构建接口对象  接口不能new 由retrofit来create
//        CarListService carLIstService = retrofit.create(CarListService.class);
//        //调用接口中的方法
//        Call<CarList> all = carLIstService.getAll();
//        //来调用Call类型中的异步访问网络的方法
//        all.enqueue(new Callback<CarList>() {
//            @Override
//            public void onResponse(Call<CarList> call, Response<CarList> response) {
//                CarList carList = response.body();
//                Log.d(TAG, "onResponse: "+carList.getResult().get(0).);
//            }
//
//            @Override
//            public void onFailure(Call<CarList> call, Throwable t) {
//                Log.d(TAG, "onFailure: "+t.getMessage());
//
//            }
//        });
//
//    }



    private void getInfo() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://zuowen.api.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //构建接口对象  接口不能new 由retrofit来create
        CarListService guzhangService = retrofit.create(CarListService.class);
        //调用接口中的方法
        Call<CarList> all = guzhangService.getAll();
        //来调用Call类型中的异步访问网络的方法
        all.enqueue(new Callback<CarList>() {
            @Override
            public void onResponse(Call<CarList> call, Response<CarList> response) {
                CarList carList=response.body();
                Log.d(TAG, "onResponse: "+carList.getResult().get(2).getName());
            }

            @Override
            public void onFailure(Call<CarList> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());

            }
        });


    }
}